//
//  DrawStarView.m
//  iOS-drawStar
//
//  Created by URANUS on 16/3/2.
//  Copyright © 2016年 Mr.X. All rights reserved.
//

#import "DrawStarView.h"

@implementation DrawStarView
{
    UIBezierPath *_startPath;
}

#pragma  mark - Overrid
- (void)drawRect:(CGRect)rect
{
    CGFloat width = CGRectGetWidth(rect);
    CGFloat height = CGRectGetHeight(rect);
    CGFloat centerX = width/2;
    CGFloat centerY = height/2;
    CGFloat radius = (width>height?height:width)/2*0.7;
    CGFloat radiusInside = (radius+tanf(M_PI*0.2)*tanf(M_PI*0.2))/(3-tanf(M_PI*0.2)*tanf(M_PI*0.2));
    //获取当前环境
    CGContextRef context = UIGraphicsGetCurrentContext();
    //保存当前环境，便于以后恢复
    CGContextSaveGState(context);
    CGContextDrawPath(context, kCGPathFillStroke);
    //绘图线条
    _startPath = [UIBezierPath bezierPath];
#ifndef StarLineWidth
#define StarLineWidth 1
#endif
    _startPath.lineWidth = self.lineWidth?self.lineWidth:StarLineWidth;
    for (int i=0; i<5; i++) {
        CGFloat outsideX = centerX+radius*cosf(M_PI*0.8*i);
        CGFloat outsideY = centerY+radius*sinf(M_PI*0.8*i);
        CGFloat insideX = centerX+radiusInside*cosf(M_PI*0.8*i+M_PI*0.4);
        CGFloat insideY = centerY+radiusInside*sinf(M_PI*0.8*i+M_PI*0.4);
        CGPoint outsidePoint = CGPointMake(outsideX, outsideY);
        CGPoint insidePoint = CGPointMake(insideX, insideY);
        if (i==0) {
            [_startPath moveToPoint:outsidePoint];
        }else{
            [_startPath addLineToPoint:outsidePoint];
        }
        [_startPath addLineToPoint:insidePoint];
    }
    CGFloat endOutsideX = centerX+radius*cosf(M_PI*0.8*0);
    CGFloat endOutsideY = centerY+radius*sinf(M_PI*0.8*0);
    CGPoint endOutsidePoint = CGPointMake(endOutsideX, endOutsideY);
    [_startPath addLineToPoint:endOutsidePoint];
    //描边
#ifndef StartStrokeColor
#define StartStrokeColor [UIColor blackColor]
#endif
    UIColor *strokeColor = self.startStrokeColor == nil ? StartStrokeColor:self.startStrokeColor;
    [strokeColor setStroke];
    [_startPath stroke];
    
    
    //填充
#ifndef StartFillColor
#define StartFillColor [UIColor yellowColor]
#endif
    UIColor *fillColor = self.startFillColor == nil ? StartFillColor:self.startFillColor;
    [fillColor setFill];
    [_startPath fill];
    //旋转
    self.transform = CGAffineTransformMakeRotation(M_PI*1.5);
    
}

#pragma mark Setter
-(void)setStartFillColor:(UIColor *)startFillColor{
    
    _startFillColor = startFillColor;
    [self setNeedsDisplay];
    
}
#pragma mark Setter
-(void)setStartStrokeColor:(UIColor *)startStrokeColor{
    
    _startStrokeColor = startStrokeColor;
    [self setNeedsDisplay];
    
}

@end
